import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { useMessage } from '@/hooks/web/useMessage';

import { getAllRoleList, isAccountExist } from '@/api/demo/system';
import { getRoleList } from '@/api/system/role';
import { ChangeStatus } from '@/api/system/user';

/**
 * transform mock data
 * {
 *  0: '华东分部',
 * '0-0': '华东分部-研发部'
 * '0-1': '华东分部-市场部',
 *  ...
 * }
 */
export const deptMap = (() => {
  const pDept = ['华东分部', '华南分部', '西北分部'];
  const cDept = ['研发部', '市场部', '商务部', '财务部'];

  return pDept.reduce((map, p, pIdx) => {
    map[pIdx] = p;

    cDept.forEach((c, cIndex) => (map[`${pIdx}-${cIndex}`] = `${p}-${c}`));

    return map;
  }, {});
})();

type CheckedType = boolean | string | number;

export const columns: BasicColumn[] = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 50,
  },
  {
    title: '账户',
    dataIndex: 'username',
    // width: 120,
  },
  // {
  //   title: '昵称',
  //   dataIndex: 'nickname',
  //   // width: 120,
  // },
  // {
  //   title: '邮箱',
  //   dataIndex: 'email',
  //   // width: 120,
  // },
  {
    title: '角色',
    dataIndex: 'roleName',
    // width: 200,
  },
  // {
  //   title: '所属部门',
  //   dataIndex: 'dept',
  //   customRender: ({ value }) => {
  //     return deptMap[value];
  //   },
  // },
  {
    title: '部门',
    dataIndex: 'deptName',
  },
  {
    title: '禁用',
    dataIndex: 'status',
    width: 80,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '正常',
        unCheckedChildren: '锁定',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          ChangeStatus(record.id, { status: newStatus })
            .then(() => {
              record.status = newStatus;
              if (newStatus == 1) {
                createMessage.success(`解除禁用`);
              } else {
                createMessage.success(`禁用成功`);
              }
            })
            .catch((err) => {
              createMessage.error(err);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  // {
  //   title: '备注',
  //   dataIndex: 'remark',
  // },
  {
    title: '登录次数',
    dataIndex: 'login_count',
    width: 80,
  },
  {
    title: '最近登录时间',
    dataIndex: 'last_login_at',
    width: 150,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'wd',
    component: 'Input',
    colProps: { span: 5 },
    componentProps: {
      placeholder: 'UID/用户名/手机号',
    },
  },
  {
    field: 'status',
    component: 'Select',
    componentProps: {
      options: [
        { label: '正常', value: 1 },
        { label: '锁定', value: 0 },
      ],
      placeholder: '状态',
      class: 'ml-1',
    },
    colProps: { span: 3 },
  },
  {
    field: 'gid',
    component: 'ApiSelect',
    componentProps: {
      api: getRoleList,
      params: { status: 1 },
      labelField: 'name',
      valueField: 'id',
      placeholder: '角色',
      class: 'ml-2',
    },
    colProps: { span: 4 },
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
      {
        trigger: 'blur',
        validator(_, value) {
          return new Promise((resolve, reject) => {
            if (!value) return resolve();
            isAccountExist(value)
              .then(resolve)
              .catch((err) => {
                reject(err.message || '验证失败');
              });
          });
        },
      },
    ],
  },
  {
    field: 'password',
    label: '密码',
    component: 'InputPassword',
    required: true,
    ifShow: false,
  },
  {
    label: '角色',
    field: 'role_id',
    component: 'ApiSelect',
    componentProps: {
      api: getRoleList,
      params: { status: 1 },
      labelField: 'name',
      valueField: 'id',
    },
    required: true,
  },
  {
    field: 'dept_id',
    label: '所属部门',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'name',
        value: 'id',
      },
      treeDefaultExpandAll: true,
      getPopupContainer: () => document.body,
    },
    required: true,
  },
  {
    field: 'nickname',
    label: '昵称',
    component: 'Input',
    required: true,
  },

  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    required: true,
  },

  {
    label: '手机号码',
    field: 'mobile',
    component: 'Input',
    required: true,
  },

  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
];
